//
//  MCReturnInfoViewController.swift
//  StoreManage
//
//  Created by MC on 2017/12/27.
//  Copyright © 2017年 MC. All rights reserved.
//

import UIKit

class MCReturnInfoViewController: MCViewController {

    var linkMan = ""
    var mobile = ""
    var province = ""
    var city = ""
    var area = ""
    var address = ""

    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        baseSetting()
        
        initUI()
        
        sendNetworking()
    }
    
    // MARK: - System Delegate
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        view.endEditing(true)
    }
    
    // MARK: - Custom Delegate
    
    // MARK: - Action
    
    @objc func saveButtonClecked() {
        
        guard nameView.textField.text?.count != 0 else {
            MCHUD_text(message: "请填写收货人姓名", view: self.view, delay: 1.5)
            return
        }
        
        guard phoneView.textField.text?.count == 11 else {
            MCHUD_text(message: "请填写正确的收货人手机号", view: self.view, delay: 1.5)
            return
        }
        
        guard provienceView.textField.text?.count != 0 else {
            MCHUD_text(message: "请选择收货地址的省/市/区", view: self.view, delay: 1.5)
            return
        }
        
        guard addressView.textField.text?.count != 0 else {
            MCHUD_text(message: "请选择收货地址详细地址", view: self.view, delay: 1.5)
            return
        }

        let array = provienceView.textField.text?.components(separatedBy: "/")
        
        if (array?.count)! < 2 {
            MCHUD_text(message: "请选择收货地址的省/市/区", view: self.view, delay: 1.5)
            return
        }
        
        let para = [
            "storeId" : MCUserinfo_getStoreId(),
            "linkMan" : nameView.textField.text ?? "",
            "mobile"  : phoneView.textField.text ?? "",
            "province": array![0],
            "city"    : array![1],
            "area"    : array![2],
            "address" : addressView.textField.text ?? ""
            ] as [String : Any]
        
        MCNetwork_post(url: JAVA_changeStoreRefundAddress, para: para as [String : AnyObject], vc: self,hudSuperView: self.view,isHiddenTabbar: true, success: { (response) in

            MCHUD_text(message: "修改退货信息成功", view: self.view, delay: 1.5)
            DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1.5, execute: {
                self.navigationController?.popViewController(animated: true)
            })
            
        }, dislike: { (response) in }) { (error) in }
    }
    
    // MARK: - Network
    func sendNetworking() {
        
        let para = [
            "data" : "1",
            ] as [String : Any]
        
        MCNetwork_post(url: JAVA_getProvinceCityArea, para: para as [String : AnyObject], vc: self,hudSuperView: nil,isHiddenTabbar: true, success: { (response) in
            let dataArray = response["data"] as! NSArray
            
            
            
            for province in dataArray {
                let provinceDict =  province as! NSDictionary
                let  proName = provinceDict["proName"]
                self.provincesArrayM.add(proName ?? "")
                
                
                // 市
                let cityM = NSMutableArray()
                let areaM = NSMutableArray()
                let cityArray = provinceDict["citys"] as! NSArray
                for city in cityArray {
                    let cityDcit = city as! NSDictionary
                    cityM.add(cityDcit["cityName"] ?? "")
                    
                    
                    // 区
                    let areaSubM = NSMutableArray()
                    let areasArray = cityDcit["areas"] as! NSArray
                    for area in areasArray {
                        let areaDict = area as! NSDictionary
                        
                        areaSubM.add(areaDict["areaName"] ?? "")
                    }
                    areaM.add(areaSubM)
                }
                
                self.citysArrayM.add(cityM)
                self.areasArrayM.add(areaM)
            }
            
            self.shippingaddressView.provinces = self.provincesArrayM
            self.shippingaddressView.citys = self.citysArrayM
            self.shippingaddressView.areas = self.areasArrayM
            
        }, dislike: { (response) in }) { (error) in }
    }
    
    // MARK: - Method
    func baseSetting() {
        self.title = "退货信息"
        self.view.backgroundColor = UIColor.white
    }
    
    func initUI() {
        view.addSubview(nameView)
        nameView.snp.remakeConstraints { (make) ->Void in
            make.left.right.equalTo(0)
            make.top.equalTo(0)
            make.height.equalTo(50)
        }
        
        view.addSubview(phoneView)
        phoneView.snp.remakeConstraints { (make) ->Void in
            make.left.right.equalTo(0)
            make.top.equalTo(nameView.snp.bottom)
            make.height.equalTo(50)
        }

        view.addSubview(provienceView)
        provienceView.snp.remakeConstraints { (make) ->Void in
            make.left.right.equalTo(0)
            make.top.equalTo(phoneView.snp.bottom)
            make.height.equalTo(50)
        }
        
        view.addSubview(addressView)
        addressView.snp.remakeConstraints { (make) ->Void in
            make.left.right.equalTo(0)
            make.top.equalTo(provienceView.snp.bottom)
            make.height.equalTo(50)
        }
        
        view.addSubview(saveButton)
        saveButton.snp.remakeConstraints { (make) ->Void in
            make.left.right.bottom.equalTo(view)
            make.height.equalTo(44)
        }
    }


// MARK: - Setter & Getter
    lazy var nameView:ItemView = {
        let view = ItemView()
        view.titleLabel.text = "收  货  人:"
        view.textField.placeholder = "填写收货人姓名"
        view.textField.text = linkMan
        return view
    }()
    
    lazy var phoneView:ItemView = {
        let view = ItemView()
        view.titleLabel.text = "手  机  号:"
        view.textField.placeholder = "填写收货人手机号码"
        view.textField.keyboardType = UIKeyboardType.numberPad
        view.textField.text = mobile
        return view
    }()
    
    lazy var provienceView:ItemView = {
        let view = ItemView()
        view.titleLabel.text = "省  市  区:"
        view.textField.placeholder = "选择省市区"
        view.textField.delegate = self
        
        if province.count != 0 {
            view.textField.text = province + "/" + city + "/" + area
        }
        
        view.textField.inputView = shippingaddressView
        return view
    }()
    
    lazy var addressView:ItemView = {
        let view = ItemView()
        view.titleLabel.text = "详细地址:"
        view.textField.placeholder = "填写详细地址"
        view.textField.text = address
        return view
    }()
    
    lazy var saveButton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        button.backgroundColor = UIColor.MCMain
        button.titleLabel?.font = MCFont(16)
        button.setTitle("保存", for: UIControlState.normal)
        button.setTitleColor(UIColor.white, for: UIControlState.normal)
        button.addTarget(self, action: #selector(saveButtonClecked), for: UIControlEvents.touchUpInside)
        return button
    }()
    
    lazy var shippingaddressView: MCShippingAddressView = {
        let view = MCShippingAddressView()
        view.delegate = self
        view.frame = CGRect.init(x: 0, y: 0, width: 0, height: 216)
        return view
    }()
    
    lazy var provincesArrayM: NSMutableArray = {
        let arrayM = NSMutableArray()
        return arrayM
    }()
    
    lazy var citysArrayM: NSMutableArray = {
        let arrayM = NSMutableArray()
        return arrayM
    }()
    
    lazy var areasArrayM: NSMutableArray = {
        let arrayM = NSMutableArray()
        return arrayM
    }()
}

extension MCReturnInfoViewController : MCShippingAddressViewDelegate {
    func MCShippingAddressViewSelected(province: String, city: String, area: String) {
        self.provienceView.textField.text = province + "/" + city + "/" + area
    }
}

extension MCReturnInfoViewController : UITextFieldDelegate {
    func textFieldDidBeginEditing(_ textField: UITextField) {
        if self.areasArrayM.count > 0 && self.citysArrayM.count > 0 && self.provincesArrayM.count > 0 {
            let provnece = self.provincesArrayM[0] as! String
            
            let cityArr = self.citysArrayM[0] as! NSArray
            let city = cityArr[0] as! String
            
            let areaArr = self.areasArrayM[0] as! NSArray
            let areaOneArr = areaArr[0] as! NSArray
            let area = areaOneArr[0] as! String
            
            self.provienceView.textField.text = provnece + "/" + city + "/" + area
        }
    }
}






class ItemView: UIView {
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.addSubview(lineView)
        lineView.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(10)
            make.right.equalTo(-10)
            make.bottom.equalTo(0)
            make.height.equalTo(1)
        }
        
        self.addSubview(titleLabel)
        titleLabel.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(15)
            make.centerY.equalTo(self)
            make.width.equalTo(70)
        }
        
        self.addSubview(textField)
        textField.snp.remakeConstraints { (make) ->Void in
            make.right.equalTo(-15)
            make.centerY.equalTo(self)
            make.left.equalTo(titleLabel.snp.right).offset(10)
            make.height.equalTo(40)
        }

    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.font = MCFont(14)
        label.textColor = UIColor.black
        return label
    }()
    
    lazy var textField: MCTextFiled = {
        let textField = MCTextFiled()
        textField.font = MCFont(14)
        textField.textAlignment = NSTextAlignment.right
        textField.textColor = UIColor.black
        textField.delegate = self
        return textField
    }()
    
    lazy var lineView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.MCGray_light
        return view
    }()
}

extension ItemView : UITextFieldDelegate {
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        
        textField.resignFirstResponder()
        return true
    }
}







